import hilog from "@ohos.hilog";

// 定义敏感信息的正则表达式
const sensitivePatterns = [
// 身份证号
  /\b\d{6}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]\b/, // 18位身份证号
  /\b\d{6}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}\b/, // 15位身份证号
  // 银行卡号(13-19位数字，支持带空格或短横线)
  /\b\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{1,5}\b/,
  // 手机号(中国大陆，支持国际格式)
  /\b(1[3-9]\d{9}\b|\+86\s?1[3-9]\d{9})\b/,
  // 邮箱地址
  /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/,
  // IPv4地址
  /\b((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\b/,
  // IPv6地址（简版）
  /\b([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b/,
  // URL链接
  /\bhttps?:\/\/[^\s]+\b/,
  // Token(28位以上字母数字组合)
  /\b[A-Za-z0-9_\-]{26,}\b/,
  // API密钥(32位字母数字组合)
  /\b[A-Za-z0-9]{32}\b/,
  // sessionID(18-48个字符)
  /\b[A-Za-z0-9]{18,48}\b/
];

export class logUtil {
  private static debugSwitch: boolean = false;
  private static TAG: string = 'rebound-..';
  private static FORMAT: string = `%{public}s`
  private static DOMAIN = 0x0001

  //过滤敏感信息的函数
  private static filterSensitiveInfo(message: string): string {
    let filteredMessage = message;
    for (const pattern of sensitivePatterns) {
      //使用全局匹配替换所有出现的敏感信息
      filteredMessage = filteredMessage.replace(new RegExp(pattern, 'g'), '******');
      return filteredMessage;
    }
  }

  public static info(...args) {
    if (this.debugSwitch) {
      const filteredMessage = this.filterSensitiveInfo(args.join(` `));
      hilog.info(this.DOMAIN, this.TAG, this.FORMAT, filteredMessage)
    }
  }

  public static debug(...args) {
    if (this.debugSwitch) {
      const filteredMessage = this.filterSensitiveInfo(args.join(` `));
      hilog.debug(this.DOMAIN, this.TAG, this.FORMAT, filteredMessage)
    }
  }

  public static error(...args) {
    if (this.debugSwitch) {
      const filteredMessage = this.filterSensitiveInfo(args.join(` `));
      hilog.error(this.DOMAIN, this.TAG, this.FORMAT, filteredMessage)
    }
  }
}